package com.neudu.yyzx_service.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yyzx_service.common.JSONReturn;
import com.neudu.yyzx_service.pojo.Nursecontent;
import com.neudu.yyzx_service.pojo.Nurselevel;
import com.neudu.yyzx_service.service.NurselevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

@RestController
public class NurselevelController {

    @Autowired
    private NurselevelService nurselevelService;
    @Autowired
    private JSONReturn jsonReturn;

    @RequestMapping("get_all_nurselevels")
    public String getAll(@RequestBody PageInfo pageInfo){
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            //可以根据数据库设置一个state字段值，匹配在住的客户查询
            List<Nurselevel> list = nurselevelService.list();
            System.out.println(list);
            PageInfo<Nurselevel> pageInfoReturn = new PageInfo<>(list);
            System.out.println(pageInfoReturn);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_nurselevels_by")
    public String getByCondition(@RequestBody Nurselevel nurselevel){
        try {
            LambdaQueryWrapper<Nurselevel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!ObjectUtils.isEmpty(nurselevel.getId()), Nurselevel::getId, nurselevel.getId())
                    .like(!ObjectUtils.isEmpty(nurselevel.getLevelName()), Nurselevel::getLevelName, nurselevel.getLevelName());
            List<Nurselevel> nurselevels = nurselevelService.list(queryWrapper);
            return jsonReturn.returnSuccess(nurselevels);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("add_one_nurselevel")
    public String addOne(@RequestBody Nurselevel nurselevel){
        try {
            nurselevelService.save(nurselevel);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getnurselevelby_id")
    public String getById(@RequestParam("nurselevelid") Integer nurselevelid){
        try {
            Nurselevel nurselevel = nurselevelService.getById(nurselevelid);
            return jsonReturn.returnSuccess(nurselevel);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifynurselevelby_id")
    public String modifyById(@RequestBody Nurselevel nurselevel){
        try {
            nurselevelService.updateById(nurselevel);
            return jsonReturn.returnSuccess(nurselevel);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removenurselevel_by_id")
    public String removeByid(@RequestParam("nurselevelid") Integer nurselevelid){
        try {
            nurselevelService.removeById(nurselevelid);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_contents_by_levelid")
    public String getContentsByLevelId(@RequestParam("nurselevelid") Integer nurselevelid){
        try {
            System.out.println("111");
            List<Nursecontent>  nursecontents =  nurselevelService.getContentsByLevelId(nurselevelid);
            return jsonReturn.returnSuccess(nursecontents);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_all_levelname")
    public String getAllName(){
        try {
            List<Nurselevel> list = nurselevelService.get_all_name();
            return jsonReturn.returnSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("update_status")
    public String updateStatus(@RequestBody Nurselevel nurselevel){
        try {
            System.out.println(nurselevel);
            if(nurselevel.getLevelStatus()==1){
                nurselevel.setLevelStatus(2);
            }else if (nurselevel.getLevelStatus()==2){
                nurselevel.setLevelStatus(1);
            }
            System.out.println(nurselevel);
            nurselevelService.updatestatus(nurselevel.getId(),nurselevel.getLevelStatus());
            return jsonReturn.returnSuccess(nurselevel);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    @PostMapping("add_item_to_level")
    public String addOneLevelItem(@RequestBody Map<Object, Object> params){
        try {
            Integer itemId = (Integer) params.get("itemId");
            Integer levelId = (Integer) params.get("levelId");
            Integer nurselevelitems = nurselevelService.addonelevelitem(levelId,itemId);
            return jsonReturn.returnSuccess(nurselevelitems);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

}
